package com.ilop.siteraudio.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.ilop.siteraudio.R;
import com.ilop.siteraudio.data.bean.RecordBean;
import com.ilop.siteraudio.data.bean.ReportBean;
import com.ilop.siteraudio.data.util.RecordDaoUtil;
import com.ilop.siteraudio.data.util.ReportDaoUtil;
import com.ilop.siteraudio.fragment.LocationFragment;
import com.ilop.siteraudio.fragment.ReportFragment;
import com.ilop.siteraudio.util.StatusBarUtil;
import com.ilop.siteraudio.view.CustomTabLayout;
import com.ilop.siteraudio.view.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2/1/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：报告页面
 */
public class ReportDetailActivity extends AppCompatActivity {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_title)
    TextView mReportTitle;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_time)
    TextView mReportTime;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_pager)
    ViewPager mViewPager;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.report_tab)
    CustomTabLayout mTabLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.iv_report_back)
    ImageView mReportBack;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.iv_report_email)
    ImageView mReportEmail;

    private long mRecordId;
    private RecordBean mRecordBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        StatusBarUtil.setDefaultStatus(this);
        ButterKnife.bind(this);
        mRecordId = getIntent().getLongExtra("recordId", 0);
        mRecordBean = RecordDaoUtil.getInstance().getRecordById(mRecordId);
        ReportBean reportBean = ReportDaoUtil.getInstance().getReportById(mRecordId);
        if (reportBean == null) {
            ReportBean report = new ReportBean();
            report.setReportId(mRecordId);
            report.setDevType("GS529");
            report.setTime(mRecordBean.getTime());
            ReportDaoUtil.getInstance().getReportDao().insert(report);
        }
        initView();
        initData();
    }

    private void initData() {
        mReportTitle.setText(mRecordBean.getTitle());
        mReportTime.setText(mRecordBean.getTime());
    }

    private void initView() {
        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(ReportFragment.newInstance(mRecordBean.getPath(), mRecordId));
        mFragments.add(LocationFragment.newInstance(mRecordId));
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.addTab(getString(R.string.report));
        mTabLayout.addTab(getString(R.string.location));
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout.getTabLayout()));
        mReportBack.setOnClickListener(view -> finish());
        mReportEmail.setOnClickListener(view -> {
            ArrayList<Integer> mRecord = new ArrayList<>();
            mRecord.add((int) mRecordId);
            Intent intent = new Intent(ReportDetailActivity.this, SendReportActivity.class);
            intent.putIntegerArrayListExtra("recordIdList", mRecord);
            startActivity(intent);
        });
    }
}
